package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.*;
import org.example.models.R;
import org.example.service.EmployeeService;
import org.example.service.SalaryStructureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.example.entity.SalaryStructure;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/salary/setup/empdisposition")
public class SalaryStructureControlle {
    @Autowired
    SalaryStructureService salaryStructureService;
    @Autowired
    EmployeeService employeeService;
    @GetMapping()
    public R<Page<SalaryStructure>> page(
            @RequestParam(name="current",defaultValue = "1") int current,
            @RequestParam(name="size",defaultValue = "10") int size,
            @RequestParam(name="emp",required = false) String emp,
            @RequestParam(name="departmental",required = false) String departmental,
            @RequestParam(name="postsName",required = false) String postsName
    ){
        QueryWrapper<SalaryStructure> queryWrapper = new QueryWrapper<>();
        if(emp != null){
            QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
            employeeQueryWrapper.lambda().like(Employee::getEmployeeName, emp);

            // 假设你有EmployeeService，可以通过它查询员工
            List<Employee> employees = employeeService.list(employeeQueryWrapper);

            // 如果查询到员工，获取他们的ID并作为条件
            if(employees != null && !employees.isEmpty()){
                List<String> employeeIds = new ArrayList<>();
                for (Employee employee : employees) {
                    Integer employeeId = employee.getEmployeeId();
                    employeeIds.add(String.valueOf(employeeId));
                }
                queryWrapper.lambda().in(SalaryStructure::getEmp, employeeIds);
            } else {
                // 如果没有查询到员工，可以直接返回空结果
                return R.success(new Page<>());
            }
        }
        if(departmental != null){
            queryWrapper.lambda().like(SalaryStructure::getDepartmental,departmental);
        }
        if(postsName != null){
            queryWrapper.lambda().like(SalaryStructure::getPostsName,postsName);
        }

        Page<SalaryStructure> page = new Page<>(current, size);

        salaryStructureService.page(page,queryWrapper);
        return R.success(page);
    }
    @PostMapping("/empdisposition")
    public R insert(@RequestBody SalaryStructure salaryStructure,@RequestAttribute("userId") Integer userId,@RequestAttribute("username") String username){
        salaryStructure.setDate(new Date());
        salaryStructure.setUpDate(new Date());
        salaryStructure.setCreatedBy(username);
        salaryStructureService.insert(salaryStructure);
        return R.success();
    }
    @PutMapping("/update")
    public R update(@RequestBody SalaryStructure salaryStructure,
                    @RequestAttribute("username") String username){
        salaryStructure.setUpDate(new Date());
        salaryStructure.setCreatedBy(username);
        salaryStructureService.updateByPrimaryKeySelective(salaryStructure);
        return R.success();
    }
    @GetMapping("/list")
    public R<List<SalaryStructure>> page(
    ){
        QueryWrapper<SalaryStructure> queryWrapper = new QueryWrapper<>();

        List<SalaryStructure> salaryStructures = salaryStructureService.list(queryWrapper);
        return R.success(salaryStructures);
    }
    @GetMapping("/ba")
    public R<List<SalaryStructure>> page(

            @RequestParam(name="emp",required = false) String emp
    ){
        QueryWrapper<SalaryStructure> queryWrapper = new QueryWrapper<>();

            queryWrapper.lambda().eq(SalaryStructure::getEmp,emp);

        List<SalaryStructure> salaryStructures = salaryStructureService.list(queryWrapper);

        return R.success(salaryStructures);
    }
}
